package com.zust513.service.impl.rrr;

import com.zust513.entity.Courier;
import com.zust513.entity.CourierRegion;
import com.zust513.entity.CourierAssistanceRequest;

import com.zust513.mapper.CourierMapper;
import com.zust513.mapper.CourierRegionMapper;
import com.zust513.mapper.OrderMapper;

import com.zust513.mapper.rrr.CourierMapperR;
import com.zust513.service.rrr.CourierServiceR;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class CourierServiceImplR implements CourierServiceR {

    @Autowired
    private CourierMapperR courierMapperR;

    @Autowired
    private CourierRegionMapper courierRegionMapperR;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Courier getCourierById(Integer courierId) {
        return courierMapperR.getCourierById(courierId);
    }

/*    @Override
    public void registerCourier(Courier courier) {
        courierMapperR.registerCourier(courier);
    }*/

    @Override
    public void cancelRegistration(Integer courierId) {
        courierMapperR.updateCourierAuditStatus(courierId, "canceled");
    }

    @Override
    public List<CourierRegion> getCourierRegions(Integer courierId) {
        return courierRegionMapperR.getCourierRegionsByCourierId(courierId);
    }

    @Override
    @Transactional
    public void sendAssistanceRequest(Integer requesterCourierId, Integer orderId) {
        // 1. 创建求援记录
        CourierAssistanceRequest request = new CourierAssistanceRequest();
        request.setRequesterCourierId(requesterCourierId);
        request.setOrderId(orderId);
        request.setRequestTime(java.time.LocalDateTime.now());
        request.setStatus("pending");

        // 2. 获取订单区域信息，设置目标区域
        // 这里简化处理，实际需要根据订单地址确定区域
        request.setTargetRegionId(1);

        // 3. 保存求援记录
        // 注：需要添加 CourierAssistanceRequestMapper 及对应方法
        // courierAssistanceRequestMapper.saveRequest(request);

        // 4. 可选择推送通知给目标区域的空闲配送员
    }

    @Override
    public List<Courier> getIdleCouriers() {
        return courierMapperR.getIdleCouriers();
    }

    @Override
    public List<Map<String, Object>> getEarningsByCourierId(Integer courierId) {
        return orderMapper.getEarningsByCourierId(courierId);
    }

    @Override
    public Map<String, Object> findUserAndCourierByUserId(Integer userId) {
        return courierMapperR.findUserAndCourierByUserId(userId);
    }

    @Override
    public List<Integer> getRegionIdsByCourierId(Integer courierId) {
        return courierMapperR.findRegionIdsByCourierId(courierId);
    }

    @Override
    public BigDecimal getBalanceByCourierId(Integer courierId) {
        return courierMapperR.getBalanceByCourierId(courierId);
    }

    @Override
    @Transactional
    public void withdraw(Integer courierId, Integer amount) {
        if (amount <= 0 || amount % 2 != 0) {
            throw new IllegalArgumentException("提现金额必须是正数且为 2 的倍数");
        }

        // 获取当前余额
        BigDecimal currentBalance = getBalanceByCourierId(courierId);
        if (currentBalance == null || currentBalance.compareTo(BigDecimal.valueOf(amount)) < 0) {
            throw new IllegalArgumentException("提现金额不能超过当前余额");
        }

        // 执行提现操作（更新 accounts 表）
        int rowsAffected = courierMapperR.updateAccountBalance(courierId, new BigDecimal(-amount));

        if (rowsAffected == 0) {
            throw new IllegalArgumentException("提现失败，未找到对应账户");
        }
    }
}

